# 创建时间:2023/2/14 23:11
# 创建人:天霄
# 计划 :SenjoTwintail
# 文件 :MusicBox.py
# 基于 Python 3.8.2
# ========================================
# 音乐音频封装
# ========================================
import pathlib
import random
from Api import savings, uiApi
from BasePygame.Public import flagBox
from BasePygame.Public.PlayMusic import Sounds, Musics


class SoundBox(Sounds):
    """播放音频"""
    BGMs = {'home': uiApi.getPathSound('mainBg'), 'directory': uiApi.getPathSound('favorBg')}

    def __init__(self):
        super().__init__()
        self.add(uiApi.getPathSound('loginBg'), 'bgm')
        self.add(uiApi.getPathSound('buttonNone'), 'click-buttonNone')
        self.add(uiApi.getPathSound('buttonNormal'), 'click-buttonNormal')
        self.add(uiApi.getPathSound('buttonLock'), 'click-buttonLock')
        self.add(uiApi.getPathSound('buttonPage'), 'click-buttonPage')
        self.add(uiApi.getPathSound('buttonReturn'), 'click-buttonReturn')
        self.add(uiApi.getPathSound('favorTouch'), 'click-favorTouch')
        self.add(uiApi.getPathSound('buttonStart'), 'click-buttonStart')
        self.add(uiApi.getPathSound('changeCloth'), 'click-changeCloth')

    def change_bgm(self, which):
        """变更bgm，并停止播放bgm"""
        self.change('bgm', self.BGMs[which])

    def play_bgm(self):
        """播放背景音乐"""
        self.play('bgm', loops=-1, fade_ms=1000, voice=savings.voice_bgm)

    def stop_bgm(self):
        """停止播放背景音乐"""
        self.stop('bgm')

    def play_click(self):
        """播放点击音效"""
        where_click_sounds = {None: 'click-buttonNone',
                              'buttonNormal': 'click-buttonNormal',
                              'buttonLock': 'click-buttonLock',
                              'buttonPage': 'click-buttonPage',
                              'buttonReturn': 'click-buttonReturn',
                              'kanban': 'click-favorTouch',
                              'buttonStart': 'click-buttonStart',
                              'changeCloth': 'click-changeCloth',
                              }
        self.play(where_click_sounds[flagBox.where_in], voice=savings.voice_bgm)
        flagBox.where_in = None

    def play_clickRight(self):
        """播放点击右键音效"""
        self.play('click-buttonReturn', voice=savings.voice_bgm)
        flagBox.playRightClick = False


class MusicBox(Musics):
    """音乐盒，一次只能播放一个音乐，建议将同一类音乐放一起"""

    def __init__(self, voice_list: list = None):
        """将同一类音乐放一起进行选择播放

        :param voice_list: 声音路径列表
        """
        super().__init__()
        self.voice_list = [] if voice_list is None else voice_list


class MusicBoxFavor(Musics):
    """音乐盒，一次只能播放一个音乐，建议将同一类音乐放一起"""

    def __init__(self, voice_list: list):
        """将同一类音乐放一起进行选择播放

        :param voice_list: 声音路径列表
        """
        super().__init__()
        self.refresh(voice_list)

    def refresh(self, voice_list: list):
        """刷新全部音乐（清空后添加）

        :param voice_list: 声音路径列表
        :return:
        """
        self.clear()
        for voice_path in voice_list:
            voice_name = pathlib.Path(str(voice_path)).name
            self.add(voice_path, voice_name)

    def playVoice(self, scene, job, soldier_code, which=None, number=''):
        """播放指定军士的音乐

        :param scene: 场景（daily，gift，touch）
        :param job: 职业（hero，soldier）
        :param soldier_code: 军士唯一代码
        :param which: 默认空，随机。哪一个，根据具体场景不同，如kanban,head,oppai
        :param number: 末尾编号，默认随机
        :return:
        """
        name = f'{scene}9{job}{soldier_code}'
        voicetmp_list = list()
        for key in self.music_dict.keys():
            key: str
            print(key)
            if key.startswith(name):
                voicetmp_list.append(key)
        if which is None:
            voicename = random.choice(voicetmp_list)
            self.play(voicename)
        elif name == '':
            name = f'{name}{which}'
            voicetmp_list2 = list()
            for key in voicetmp_list:
                if key.startswith(name):
                    voicetmp_list2.append(key)
            voicename = random.choice(voicetmp_list2)
            self.play(voicename)
            del voicetmp_list2
        else:
            voicename = f'{name}{which}{number}.mp3'
            self.play(voicename)
        del voicetmp_list
